{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 递归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "# 3 + 2 + 1\n",
    "def sum_numbers(num):\n",
    "    # 1.如果是1，直接返回1 -- 出口\n",
    "    if num == 1:\n",
    "        return 1\n",
    "    # 2.如果不是1，重复执行累加并返回结果\n",
    "    return num + sum_numbers(num-1)\n",
    "\n",
    "\n",
    "sum_result = sum_numbers(3)\n",
    "# 输出结果为6\n",
    "print(sum_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## lambda 表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<function fn1 at 0x000002EC535E1488>\n",
      "200\n",
      "<function <lambda> at 0x000002EC535E1730>\n",
      "100\n"
     ]
    }
   ],
   "source": [
    "# 函数\n",
    "def fn1():\n",
    "    return 200\n",
    "\n",
    "\n",
    "print(fn1)\n",
    "print(fn1())\n",
    "\n",
    "\n",
    "# lambda表达式\n",
    "fn2 = lambda: 100\n",
    "print(fn2)\n",
    "print(fn2())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "def add(a, b):\n",
    "    return a + b\n",
    "\n",
    "\n",
    "result = add(1, 2)\n",
    "print(result)\n",
    "\n",
    "fn1 = lambda a, b: a + b\n",
    "print(fn1(1, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 参数形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "fn1 = lambda: 100\n",
    "print(fn1())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn1 = lambda a: a\n",
    "fn1('hello world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "130"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn1 = lambda a, b, c=100: a + b + c\n",
    "fn1(10, 20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10, 20, 30)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn1 = lambda *args: args\n",
    "fn1(10, 20, 30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn1(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'age': 20, 'name': 'python'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn1 = lambda **kwargs: kwargs\n",
    "fn1(name='python', age=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'python'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fn1(name='python',)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### lambda 的应用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000\n"
     ]
    }
   ],
   "source": [
    "fn1 = lambda a, b: a if a > b else b\n",
    "print(fn1(1000, 500))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': 'Jack', 'age': 22}, {'name': 'ROSE', 'age': 19}, {'name': 'TOM', 'age': 20}]\n",
      "[{'name': 'TOM', 'age': 20}, {'name': 'ROSE', 'age': 19}, {'name': 'Jack', 'age': 22}]\n",
      "[{'name': 'ROSE', 'age': 19}, {'name': 'TOM', 'age': 20}, {'name': 'Jack', 'age': 22}]\n"
     ]
    }
   ],
   "source": [
    "students = [\n",
    "    {'name': 'TOM', 'age': 20},\n",
    "    {'name': 'ROSE', 'age': 19},\n",
    "    {'name': 'Jack', 'age': 22}\n",
    "]\n",
    "\n",
    "# 按name值升序排列\n",
    "students.sort(key=lambda x: x['name'])\n",
    "print(students)\n",
    "\n",
    "# 按name值降序排列\n",
    "students.sort(key=lambda x: x['name'], reverse=True)\n",
    "print(students)\n",
    "\n",
    "# 按age值升序排列\n",
    "students.sort(key=lambda x: x['age'])\n",
    "print(students)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 高阶函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "def add_num(a, b):\n",
    "    return abs(a) + abs(b)\n",
    "\n",
    "\n",
    "result = add_num(-1, 2)\n",
    "print(result)  # 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "def sum_num(a, b, f):\n",
    "    return f(a) + f(b)\n",
    "\n",
    "\n",
    "result = sum_num(-1, 2, abs)\n",
    "print(result)  # 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 内置高阶函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<map object at 0x000001642541E588>\n",
      "[1, 4, 9, 16, 25]\n"
     ]
    }
   ],
   "source": [
    "list1 = [1, 2, 3, 4, 5]\n",
    "\n",
    "\n",
    "def func(x):\n",
    "    return x ** 2\n",
    "\n",
    "\n",
    "result = map(func, list1)\n",
    "\n",
    "print(result)  # <map object at 0x0000013769653198>\n",
    "print(list(result))  # [1, 4, 9, 16, 25]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n"
     ]
    }
   ],
   "source": [
    "import functools\n",
    "\n",
    "list1 = [1, 2, 3, 4, 5]\n",
    "\n",
    "\n",
    "def func(a, b):\n",
    "    return a + b\n",
    "\n",
    "\n",
    "result = functools.reduce(func, list1)\n",
    "\n",
    "print(result)  # 15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<filter object at 0x00000164253A5EF0>\n",
      "[2, 4, 6, 8, 10]\n"
     ]
    }
   ],
   "source": [
    "list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
    "\n",
    "\n",
    "def func(x):\n",
    "    return x % 2 == 0\n",
    "\n",
    "\n",
    "result = filter(func, list1)\n",
    "\n",
    "print(result)  # <filter object at 0x0000017AF9DC3198>\n",
    "print(list(result))  # [2, 4, 6, 8, 10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[18, 9, 24, 12, 27]\n",
      "[14, 46, 28, 54, 44, 58, 26, 34, 64]\n"
     ]
    }
   ],
   "source": [
    "foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]\n",
    "print(list(filter(lambda x: x % 3 == 0, foo)))\n",
    "# [18, 9, 24, 12, 27]\n",
    "\n",
    "print(list(map(lambda x: x * 2 + 10, foo)))\n",
    "# [14, 46, 28, 54, 44, 58, 26, 34, 64]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "139\n"
     ]
    }
   ],
   "source": [
    "import functools\n",
    "print(functools.reduce(lambda x, y: x + y, foo))\n",
    "# 139"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:root] *",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
